package com.jwaoo.mobile.test.model;
import android.content.Context;
import android.util.Log;

import http.IHttpListener;
import com.dx.as.common.ILoadListener;
import com.dx.as.common.JWResult;
import com.dx.as.db.IDBListener;
import com.dx.as.db.SqlLiteHelper;
import com.dx.as.utils.Utils;
import com.google.gson.reflect.TypeToken;
import com.jwaoo.mobile.db.AppDatabase;
import com.jwaoo.mobile.test.bean.Story;
import com.jwaoo.mobile.test.bean.Storys;
import com.jwaoo.mobile.test.model.net.HttpService;

import java.util.List;

import http.HttpClientHelper;
import retrofit2.Call;

/**
 * Created by jwaoo on 2017/5/26.
 */

public class StorysModel extends IHttpListener {

    private static StorysModel model;
    private static ILoadListener listener;
    private static HttpService localService;
    private static Context mContext;
    static AppDatabase database;

    public StorysModel(ILoadListener loadListener, Context context) {
        super(context,loadListener);
    }

    @Override
    public void getData(Object... objects) {

    }

    public static StorysModel getInit(ILoadListener loadListener, Context context){
        if (model == null){
            synchronized (StorysModel.class){
                if (model == null){
                    model = new StorysModel(loadListener,context);
                    listener = loadListener;
                    mContext = context;
                    try {
                    localService = HttpClientHelper.getRetrofit().create(HttpService.class);
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
        }
        return model;
    }

    public void getInfo(){
        doNet(false);
    }

    @Override
    public void onResponse(Object response , int flg) {
        if (!Utils.isEmpty(response)){
            final Storys object = (Storys) response;
            if (object != null){
                listener.onResponse(object.getData(),flg);
//                        database.dBStorysDao().insertAll(object.getData());
                IDBListener<AppDatabase> idbListener = new IDBListener<AppDatabase>() {
                    @Override
                    public void onOption(AppDatabase database) {
                        List<Story> list = database.dBStorysDao().getStoryList();
                        database.dBStorysDao().deleteAll(list);
                        database.dBStorysDao().insertAll(object.getData());
                        Log.e("----","----");
                    }
                };
                SqlLiteHelper.getInstance(mContext.getApplicationContext()).dbOption(AppDatabase.class,idbListener);
            }
        }
    }

    @Override
    public void onFailure(Throwable t) {
        loadFromLocal();
//        listener.onFailure(t);
    }

    public void doNet(boolean fromLocal){
        if (fromLocal){
            loadFromLocal();
        }else {
            Call call = localService.listRepos();
            HttpClientHelper.doNet(call,this,new TypeToken<JWResult<Storys>>(){});
        }
    }

    public void loadFromLocal(){
        IDBListener<AppDatabase> idbListener = new IDBListener<AppDatabase>() {
            @Override
            public void onOption(AppDatabase database) {
                final List<Story> storyList = database.dBStorysDao().getStoryList();
                if (storyList != null && storyList.size() > 0) {
                    listener.onResponse(storyList,0);
                }
                for (Story entity : storyList){
                    Log.e("----","----"+entity.getName());
                }
            }
        };
        SqlLiteHelper.getInstance(mContext.getApplicationContext()).dbOption(AppDatabase.class,idbListener);
    }

}
